package org.gc.webapp.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

public class CharacterEncodingFilter implements Filter {

    private String encoding = null;

    private boolean forceEncoding = false;

    public CharacterEncodingFilter() {
        super();
    }

    public void init(FilterConfig config) throws ServletException {
        this.encoding = config.getInitParameter("encoding");
        if ((config.getInitParameter("forceEncoding")).equals("true")) {
            this.forceEncoding = true;
        }
    }

    public final void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) throws ServletException,
            IOException {             
        
        if (forceEncoding) {
            request.setCharacterEncoding(this.encoding);
        }
        filterChain.doFilter(request, response);
    }

    public void destroy() {
        this.encoding = null;
    }
}